package news;



import java.util.ArrayList;
import java.util.List;

import utility.Check_Net;
import utility.DbConnect;
import utility.SDialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import example.demo_navi.R;

public class Pracrss extends Activity {

	private Pracrss local;
	DbConnect dbc;
	ArrayList<Rss_item> ari;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        dbc=new DbConnect(getApplicationContext());
        String links="http://gametv.vn/feed";
        ari=new ArrayList<Rss_item>();
       local=this;
       GetRSSDataTask gr=new GetRSSDataTask();
       gr.execute(links);
    }

    private class GetRSSDataTask extends AsyncTask<String, Void, List<Rss_item>>{

    	ProgressDialog pd;
    	
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			pd=new ProgressDialog(Pracrss.this);
			pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			pd.setMessage("Loading News...");
			pd.show();
		}

		@Override
		protected List<Rss_item> doInBackground(String... urls) {
			// TODO Auto-generated method stub
		     try {
	                if(Check_Net.isOnLine(Pracrss.this))	{    	
	                Rss_read rssReader = new Rss_read(urls[0]);
	                for(Rss_item i:rssReader.getItems()){
	                	dbc.insert_news(new Rss_item(i.getTitle(),i.getLink(),i.getDes(),i.getPubdate()));
	                	Log.d("Db_Error", i.getTitle() + " - " +i.getDes() +" - "+ i.getPubdate());
	                	String vb=i.getTitle();
	                	String vc=i.getLink();
	                	String vd=i.getDes();
	                	String ve=i.getPubdate();
	                	Rss_item af=new Rss_item(vb, vc, vd, ve);
	                	ari.add(af);
	                	Log.d("Man Chi", ""+ari);
	                 }
	                
	                return rssReader.getItems();}
	                else{
	                	SDialog.Dialognet(Pracrss.this);
	                }
	             
	            } catch (Exception e) {
	                Log.e("RssChannelActivity", e.getMessage());
	            }
	             
	            return null;
		}

		@Override
		protected void onPostExecute(List<Rss_item> result) {
			// TODO Auto-generated method stub
			pd.dismiss();
			ari=dbc.getAllNews();
            ListView itcItems = (ListView) findViewById(R.id.lv1);
       //     result=dbc.getAllNews();
            for(Rss_item ri:result){
            	Log.d("Mabiqia","title: "+ri.getId()+ri.getTitle());
            }
            
            ArrayAdapter<Rss_item> adapter = new ArrayAdapter<Rss_item>(local,android.R.layout.simple_list_item_1, ari);
            itcItems.setAdapter(adapter);
                         
            itcItems.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					
					Log.d("bibi", ""+ari);
					final Uri myLink =  Uri.parse(ari.get(arg2).getLink());
					AlertDialog.Builder adb=new AlertDialog.Builder(Pracrss.this);
					adb.setTitle("hello");
					adb.setMessage( ari.get(arg2).getTitle() + "\n\n" 			    		    
			    		 	+ ari.get(arg2).getDes() + "\n" );
					adb.setPositiveButton("close", null);
					adb.setNegativeButton("More", new DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface arg0, int arg1) {
							// TODO Auto-generated method stub
							Intent i=new Intent(Intent.ACTION_VIEW,myLink);
							startActivity(i);
						}
					});
					adb.show();
				}
            	
			});
            Log.d("Mabiqia2","title: "+result);
          
		}
    	
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_demo_navi, menu);
        return true;
    }
}
